//
//  PPProfileSettingViewController.m
//  pickpic
//
//  Created by Jinxiao on 8/27/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPProfileSettingViewController.h"

#import "PPAvatarSettingPreviewViewController.h"
#import "PPSettingBirthdayDatePicker.h"
#import "PPSettingLocationPicker.h"
#import "PPSettingPicCell.h"
#import "PPSettingCell.h"
#import "PPAccTypeCell.h"
#import "PPCacheCell.h"
#import "PPNicknameSettingViewController.h"
#import "PPGenderSettingViewController.h"
#define kSettingFont ([UIFont boldSystemFontOfSize:16])
@interface PPProfileSettingViewController ()<PPSettingBirthdayDatePickerDelegate, PPSettingLocationPickerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *avatarView;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *signatureLabel;
@property (strong, nonatomic) IBOutlet UILabel *accountLabel;
@property (strong, nonatomic) IBOutlet UILabel *genderLabel;
@property (strong, nonatomic) IBOutlet UILabel *birthdayLabel;
@property (strong, nonatomic) IBOutlet UILabel *locationLabel;
@property (weak, nonatomic) IBOutlet UIView *line1;
@property (weak, nonatomic) IBOutlet UIView *line2;
@property (weak, nonatomic) IBOutlet UIView *line3;
@property (weak, nonatomic) IBOutlet UIView *line4;
@property (weak, nonatomic) IBOutlet UIView *line5;
@property (weak, nonatomic) IBOutlet UIView *line6;
@property (weak, nonatomic) IBOutlet UIView *line7;
@property (weak, nonatomic) IBOutlet UIView *line8;
@property (weak, nonatomic) IBOutlet UIView *line9;


@property (nonatomic,strong) NSArray *subtitles;
@property (nonatomic,copy) NSString *photoURL;
@property (nonatomic,copy) NSString *userName;
@property (nonatomic,copy) NSString *pickNum;
@property (nonatomic,copy) NSString *sex;

@end

@implementation PPProfileSettingViewController
static NSString *const picCellID = @"SettingPicCell";
static NSString *const setCellID = @"SettingCell";
static NSString *const accCellID = @"AccTypeCell";
static NSString *const cacheCellID = @"CacheCell";

- (void)viewDidLoad {
    [super viewDidLoad];

//    [_line1 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
//    [_line2 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
//    [_line3 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
//    [_line4 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
//    [_line5 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
//    [_line6 addTopBorderWithColor:kLineColor andWidth:kLineWidth];
//    [_line7 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
//    [_line8 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];
//    [_line9 addBottomBorderWithColor:kLineColor andWidth:kLineWidth];

    self.view.backgroundColor = WHITE;
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:@"设置" image:@"nav_back" target:self action:@selector(popViewPreviousController)];
    
    self.navigationController.navigationBar.tintColor = BLACK;
    
    self.subtitles = @[@"头像",@"昵称",@"pickpic号",@"性别"];
    
    [self registerCell];
}
- (void)registerCell{
    
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 14, 0, 0);
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PPSettingPicCell class]) bundle:nil] forCellReuseIdentifier:picCellID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PPSettingCell class]) bundle:nil] forCellReuseIdentifier:setCellID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PPAccTypeCell class]) bundle:nil] forCellReuseIdentifier:accCellID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PPCacheCell class]) bundle:nil] forCellReuseIdentifier:cacheCellID];
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];


    [self reloadDatas];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self reloadDatas];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.subtitles.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        PPSettingPicCell *picCell = [self.tableView dequeueReusableCellWithIdentifier:picCellID];
        picCell.leftLabel.text = self.subtitles[indexPath.row];
        picCell.leftLabel.font = kSettingFont;
        UIImage *placeholderImg = [UIImage imageNamed:@"person_normal"];
        
        if (self.photoURL.length > 0) {
            [picCell.picImage sd_setImageWithURL:[NSURL URLWithString:self.photoURL] placeholderImage:placeholderImg];
        }else{
            picCell.picImage.image = placeholderImg;
        }
        
        return picCell;
        
    }else if (indexPath.row == 1){
        
        PPAccTypeCell *nickCell = [self.tableView dequeueReusableCellWithIdentifier:accCellID];
        
         nickCell.leftLabel.text = self.subtitles[indexPath.row];
        nickCell.nickName.text = self.userName;
        nickCell.leftLabel.font = kSettingFont;
        return nickCell;
        
        
    }else if (indexPath.row == 2){
        
        PPCacheCell *cacheCell = [self.tableView dequeueReusableCellWithIdentifier:cacheCellID];
        cacheCell.leftLabel.text = self.subtitles[indexPath.row];
        cacheCell.leftLabel.font = kSettingFont;
        cacheCell.cacheLabel.text = self.pickNum;
        return cacheCell;
        
    }else{
        PPAccTypeCell *sexCell = [self.tableView dequeueReusableCellWithIdentifier:accCellID];
        sexCell.leftLabel.text = self.subtitles[indexPath.row];
        sexCell.nickName.text = self.sex;
        sexCell.leftLabel.font = kSettingFont;
        return sexCell;
    }
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == 0) {

        PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;

        PPAvatarSettingPreviewViewController *previewViewController = [[PPAvatarSettingPreviewViewController alloc] init];
        previewViewController.avatar = profiles.avatarURLString;
        [self.navigationController pushViewController:previewViewController animated:YES];
    }
    else if (indexPath.row == 3)
    {

        PPGenderSettingViewController *genderVc = [[UIStoryboard storyboardWithName:@"Setting" bundle:nil] instantiateViewControllerWithIdentifier:@"PPGenderSettingViewController"];
        
        [self.navigationController pushViewController:genderVc animated:YES];
        
//        PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
//        PPSettingBirthdayDatePicker *picker = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([PPSettingBirthdayDatePicker class]) owner:nil options:nil][0];
//        picker.delegate = self;
//        picker.selectedDateString = [[profiles.birthday validString] length] ? profiles.birthday : @"1990年1月1日";
//        [picker showInView:self.navigationController.view];
    }
    else if (indexPath.row == 1)
    {
//        PPNicknameSettingViewController *nikeVc = [[UIStoryboard storyboardWithName:@"Setting" bundle:nil] instantiateViewControllerWithIdentifier:@"PPNicknameSettingViewController"];
          PPNicknameSettingViewController *nikeVc = [[PPNicknameSettingViewController alloc] init];
        [self.navigationController pushViewController:nikeVc animated:YES];
//        //地区
//        PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
//        PPSettingLocationPicker *picker = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([PPSettingLocationPicker class]) owner:nil options:nil][0];
//        picker.delegate = self;
//        [picker setDefaultState:profiles.state city:profiles.city];
//        [picker showInView:self.navigationController.view];
    }
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 15;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    return CGFLOAT_MIN;
//}

- (void)reloadDatas {

    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;

    if(profiles.avatarURLString.length > 0) {
//        [profiles.avatarURLString applyToImageView:_avatarView];
        self.photoURL = profiles.avatarURLString;
    } else {
        _avatarView.image = [UIImage imageNamed:@"person_normal"];
    }
    
    _nameLabel.text = profiles.name;
    _signatureLabel.text = profiles.signature ?: @"";
    _accountLabel.text = profiles.account;
    
    self.userName = profiles.name;

//    self.pickNum = profiles.signature ?: @"";
    self.pickNum = [PPUserAccount currentUser].pickpic;

    switch(profiles.gender.integerValue) {
        case PPUserGenderUnknown: {
           _genderLabel.text = @"";
            self.sex = @"";
            break;
        }

        case PPUserGenderFemale: {
            _genderLabel.text = @"女";
            self.sex = @"女";
            break;
        }

        case PPUserGenderMale: {
           _genderLabel.text = @"男";
            self.sex = @"男";
            break;
        }
    }
    
//    self.sex = _genderLabel.text;

    _birthdayLabel.text = profiles.birthday ?: @"";
    _locationLabel.text = profiles.city ? [NSString stringWithFormat:@"%@ %@", profiles.state, profiles.city] : @"";
    
    [self.tableView reloadData];
}

#pragma mark - PPSettingBirthdayDatePickerDelegate
- (void)didUpdateBirthdaySuccessfull:(NSString *)newBirthday
{
    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    profiles.birthday = newBirthday;
    [profiles.managedObjectContext saveContext];

    _birthdayLabel.text = newBirthday;
}

#pragma mark - PPSettingLocationPickerDelegate
- (void)didUpdateLocationSuccessfull:(NSString *)newProvince newCity:(NSString *)newCity
{
    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    profiles.state = newProvince;
    profiles.city = newCity;

    [profiles.managedObjectContext saveContext];

    _locationLabel.text = [NSString stringWithFormat:@"%@ %@", newProvince, newCity];
}
@end
